var express = require('express');
var router = express.Router();
const cates = require('../application/articleCate')
const about = require('../application/about')
const articles = require('../application/article')
const ResponseBody = require('../types/ResponseBody');
const { login } = require('../application/user')

//之后可引入分块路由
router.get('/queryCategories', async (req, res, next) => {
    try {
        const data = await cates.queryCategories();
        res.json(new ResponseBody(true, data, "获取成功"));

    } catch (e) {
        console.log(e);
        res.json(new ResponseBody(false, null, "获取失败"));  //改为发送错误
    }
});


router.get('/queryAbout', async (req, res, next) => {
    try {
        const data = await about.queryAbout();
        res.json(new ResponseBody(true, data, "获取成功"));

    } catch (e) {
        console.log(e);
        res.json(new ResponseBody(false, null, "获取失败"));  //改为发送错误
    }
});

router.get('/queryAllArticles', async (req, res, next) => {
    try {
        const data = await articles.queryAllArticles();
        res.json(new ResponseBody(true, data, "获取成功"));
    } catch (e) {
        console.log(e);
        res.json(new ResponseBody(false, null, "获取失败"));  //改为发送错误
    }
});

router.get('/queryArticleOnID', async (req, res, next) => {
    try {
        const data = await articles.queryArticleOnID(req.query.articleID);
        res.json(new ResponseBody(true, data, "获取成功"));
    } catch (e) {
        console.log(e);
        res.json(new ResponseBody(false, null, "获取失败"));  //改为发送错误
    }
});

router.get('/queryArticlesOnCate', async (req, res, next) => {
    try {
        const data = await articles.queryArticlesOnCate(req.query.cateID);
        res.json(new ResponseBody(true, data, "获取成功"));
    } catch (e) {
        console.log(e);
        res.json(new ResponseBody(false, null, "获取失败"));  //改为发送错误
    }
});



// 登录接口
router.post('/login', async function (req, res, next) {
    //检查参数
    if (!req.body.name || !req.body.password) {
        return res.json(new ResponseBody(false, null, "请求错误"));
    }
    const name = req.body.name;
    const password = req.body.password;
    const user = await login(name, password);    //验证用户名密码是否正确
    if (user) {
        req.session.regenerate(function (err) {
            if (err) {
                return res.json(new ResponseBody(false, null, "登录失败"));
            }
            req.session.loginUser = user.name;
            res.json(new ResponseBody(true, null, "登陆成功"));
        });
    } else {
        res.json(new ResponseBody(false, null, "帐号或密码错误"));
    }
});

//本地检查是否登录
const req_pass = (req) => {
    const sess = req.session;
    const loginUser = sess.loginUser;
    return !!loginUser;
}
//接口：是否登录检查
router.get('/checkLogin', async function (req, res, next) {

    if (req_pass(req)) {
        res.json(new ResponseBody(true, null, "已经登陆"));
    } else {
        res.json(new ResponseBody(false, null, "未登录"));
    }
});

//上传新的文章
router.post('/addArticle', async function (req, res, next) {

    if (req_pass(req)) {
        const { markdown, title, cateID } = req.body;    //提取参数
        if (typeof (markdown && title && cateID) === 'undefined'
            ||
            parseInt(cateID) == NaN || title == '') {   //参数非法
            res.json(new ResponseBody(false, null, "请求错误"));
        }
        //参数合法，调用服务层
        try {
            const ifSuccess = articles.addArticle(title, markdown, parseInt(cateID));
            if (ifSuccess) {
                res.json(new ResponseBody(true, null, "添加成功"));
            }
        } catch (e) {
            console.log(e)
            res.json(new ResponseBody(false, null, "系统错误"));
        }
    } else {
        res.json(new ResponseBody(false, null, "未登录"));
    }
});

//编辑文章
router.post('/editArticle', async function (req, res, next) {

    if (req_pass(req)) {
        const { markdown, title, cateID, articleID } = req.body;    //提取参数
        if (typeof (markdown && title && cateID && articleID) === 'undefined'
            ||
            parseInt(cateID) == NaN || title == '' || parseInt(articleID) == NaN) {   //参数非法
            res.json(new ResponseBody(false, null, "请求错误"));
        }
        //参数合法，调用服务层
        try {
            const ifSuccess = await articles.editArticle(title, markdown, parseInt(cateID), parseInt(articleID));
            if (ifSuccess) {
                res.json(new ResponseBody(true, ifSuccess, "编辑成功"));
            }
        } catch (e) {
            console.log(e)
            res.json(new ResponseBody(false, null, "系统错误"));
        }

    } else {
        res.json(new ResponseBody(false, null, "未登录"));
    }
});


//删除文章
router.get('/deleteArticle', async function (req, res, next) {

    if (req_pass(req)) {
        const { articleID } = req.query;    //提取参数
        if (typeof (articleID) === 'undefined'
            ||
            parseInt(articleID) == NaN) {   //参数非法
            res.json(new ResponseBody(false, null, "请求错误"));
        }
        //参数合法，调用服务层
        try {
            const ifSuccess = await articles.deleteArticle(parseInt(articleID));
            if (ifSuccess) {
                res.json(new ResponseBody(true, ifSuccess, "删除成功"));
            }
        } catch (e) {
            console.log(e)
            res.json(new ResponseBody(false, null, "系统错误"));
        }

    } else {
        res.json(new ResponseBody(false, null, "未登录"));
    }
});
module.exports = router;
